package com.zhz.lib.project.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhz.lib.project.domain.hotlink.Hotlink;
import com.zhz.lib.project.domain.news.Sortmap;
import com.zhz.lib.project.service.custom.CustomService;
import com.zhz.lib.project.service.hotlink.HotlinkService;
import com.zhz.lib.project.service.news.NewsService;
import com.zhz.lib.project.vo.custom.CustomVO;
import com.zhz.lib.project.vo.news.NewsVO;
import com.zhz.project.common.util.hibernate.BaseDAO;

@Controller
@RequestMapping("/index.do")
public final class IndexController {
    private static Logger  logger      = Logger.getLogger(IndexController.class);
    private static int     firstResult = 0;
    private static int     maxResults  = 10;
    @Autowired
    private NewsService    newsService;
    @Autowired
    private HotlinkService hotlinkService;
    @Autowired
    private CustomService  customService;
    @Autowired
    private BaseDAO        baseDao;

    @SuppressWarnings("unchecked")
    @RequestMapping(method = RequestMethod.GET)
    public String get(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
        Sortmap smp = (Sortmap) baseDao.findOneObject("Sortmap", "type", "rlink");
        if (smp != null) {
            // 获取相关链接
            List<Hotlink> rlinkList = hotlinkService.getHotLink(smp.getSortMapId() + "");
            model.put("rlinkEntrySet", getHotlinkMap(rlinkList).entrySet());
        }

        smp = (Sortmap) baseDao.findOneObject("Sortmap", "type", "hlink");
        if (smp != null) {
            // 获取常用链接
            List<Hotlink> hlinkList = hotlinkService.getHotLink(smp.getSortMapId() + "");
            model.put("hlinkEntrySet", getHotlinkMap(hlinkList).entrySet());
        }
        
        // 获取最新消息
        List<NewsVO> newsVOList = newsService.getLatestNews(firstResult, maxResults);
        model.put("newsVOList", newsVOList);

        // 获取顶级模块以及对应的子模块
        List<CustomVO> customVOList = customService.getTopCustom();
        if (customVOList != null && customVOList.size() > 0)
            for (CustomVO customVO : customVOList) {
                customVO
                    .setSubCustomVOList(customService.getSubCustom(customVO.getCustomId() + ""));
            }
        
        CustomVO firstCustomVO = null;
        CustomVO lastCustomVO = null;
        if(customVOList.size()>0){
        	 firstCustomVO = customVOList.get(0);
        	 lastCustomVO = customVOList.get(customVOList.size() - 1);
        }
        if(customVOList.size()>1){
        	customVOList = customVOList.subList(1, customVOList.size() - 1);
        }
        model.put("firstCustomVO", firstCustomVO);
        model.put("lastCustomVO", lastCustomVO);
        model.put("customVOList", customVOList);
        return "index";
    }

    /**
     * 根据排序值进行分类
     *
     * @param linkList
     * @return
     */
    private Map<Integer, List<Hotlink>> getHotlinkMap(List<Hotlink> linkList) {
        Map<Integer, List<Hotlink>> map = new LinkedHashMap<Integer, List<Hotlink>>();
        if (linkList != null && !linkList.isEmpty()) {
            for (Hotlink hotlink : linkList) {
                List<Hotlink> list = map.get(hotlink.getSortNumber());
                if (list != null) {
                    list.add(hotlink);
                } else {
                    List<Hotlink> temp = new ArrayList<Hotlink>();
                    temp.add(hotlink);
                    map.put(hotlink.getSortNumber(), temp);
                }
            }
        }
        return map;
    }

    @RequestMapping(method = RequestMethod.GET, params = "event=thanks")
    public String thanks(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
        return "thanks";
    }
}
